//
//  CodeScanViewController.m
//  MJAVFoundationDemo
//
//  Created by apple on 16/9/21.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "CodeScanViewController.h"
#import<AVFoundation/AVFoundation.h>
@interface CodeScanViewController ()<AVCaptureMetadataOutputObjectsDelegate>
@property(nonatomic,strong)AVCaptureSession *session;//输入输出之间的桥梁，捕获会话
@property(nonatomic,strong)UIView *scanView;//显示二维码的视图
@property(nonatomic,strong)UIView *lineView;
@end

@implementation CodeScanViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor lightGrayColor];
    self.title=@"二维码扫描";
    [self.view addSubview:self.scanView];
    [self.view addSubview:self.lineView];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.scanBtn.hidden=NO;
    
}
-(UIView *)scanView
{
    if (!_scanView) {
        _scanView=[[UIView alloc]init];
        _scanView.backgroundColor=[UIColor whiteColor];
        _scanView.bounds=CGRectMake(0,0,200, 200);
        _scanView.center=CGPointMake(self.view.center.x, self.view.center.y+64);
        _scanView.hidden=YES;
        _scanView.layer.borderColor=[UIColor blackColor].CGColor;
        _scanView.layer.borderWidth=1.0;
    }
    return _scanView;
}
-(UIView *)lineView
{
    if (!_lineView) {
        _lineView=[[UIView alloc]init];
        _lineView.backgroundColor=[UIColor greenColor];
        _lineView.frame=CGRectMake(CGRectGetMinX(_scanView.frame),CGRectGetMinY(_scanView.frame), CGRectGetWidth(_scanView.frame), 5);
        _lineView.hidden=YES;
     
    }
    return _lineView;
}
- (IBAction)startScan:(id)sender
{
       //获取设备
    AVCaptureDevice *device=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //输入流
    NSError *error;
    AVCaptureDeviceInput *inPut=[AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (!inPut)
    {
        [MBProgressHUD showError:@"请用真机测试"];
        return;
    }
    if (error)
    {
        NSLog(@"DeviceInputError=%@",error.localizedDescription);
    }
    UIButton *btn=sender;
    btn.hidden=YES;
    _lineView.hidden=NO;
    _scanView.hidden=NO;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationRepeatCount:100];
    [UIView setAnimationRepeatAutoreverses:YES];
    CGRect rect=_lineView.frame;
    rect.origin.y=CGRectGetMaxY(self.scanView.frame)-5;
    _lineView.frame=rect;
    [UIView commitAnimations];

    //输出流
    AVCaptureMetadataOutput *outPut=[[AVCaptureMetadataOutput alloc]init];
    //在主线程中刷新
    [outPut setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    //设置扫码支持的编码格式，这里是二维码
    
    _session=[[AVCaptureSession alloc]init];
    //设置采集率
    [_session setSessionPreset:AVCaptureSessionPresetHigh];
    [_session addInput:inPut];
    [_session addOutput:outPut];
    
    outPut.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];
    
    AVCaptureVideoPreviewLayer *layer=[AVCaptureVideoPreviewLayer layerWithSession:_session];
    layer.videoGravity=AVLayerVideoGravityResizeAspectFill;
    layer.frame=self.scanView.layer.bounds;
    [self.scanView.layer insertSublayer:layer atIndex:0];
    [_session startRunning];
    
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    if (metadataObjects.count>0)
    {
        [_session stopRunning];
        [_lineView.layer removeAllAnimations];
        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];
        //输出扫描字符串
        NSLog(@"---%@",metadataObject);

        NSLog(@"---%@",metadataObject.stringValue);
//        WebViewController *webView=[[WebViewController alloc]init];
//        webView.urlString=metadataObject.stringValue;
//        [self presentViewController:webView animated:YES completion:nil];
    }

}
@end
